package me.yiyou.meihua.ui.page.main

import androidx.compose.foundation.Image
import androidx.compose.foundation.LocalIndication
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import me.yiyou.meihua.R
import me.yiyou.meihua.ui.screen.Screen

@Composable
fun MainPage(navController: NavController) {
    Column(Modifier
        .fillMaxWidth()
        .padding(top = 160.dp)) {
        Row(
            modifier = Modifier
                .fillMaxWidth(),
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
            TabItem(
                "软件说明",
                R.drawable.icon_meihua,
                onClick = { navController.navigate(Screen.Describe.route) })
            TabItem(
                "顺序记忆",
                R.drawable.icon_shunxu,
                onClick = { navController.navigate(Screen.Memory.route) })
            TabItem(
                "64卦象",
                R.drawable.icon_bagua,
                onClick = { navController.navigate(Screen.Study.route) })
            TabItem(
                "时间起卦",
                R.drawable.icon_date,
                onClick = { navController.navigate(Screen.DateQiGua.route) })
        }
        Row(
            modifier = Modifier
                .fillMaxWidth(),
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
            TabItem(
                "起卦记录",
                R.drawable.history,
                onClick = { navController.navigate(Screen.Record.route) })
            TabItem(
                "随机起卦",
                R.drawable.icon_random,
                onClick = { navController.navigate(Screen.RandomPre.route) })
            TabItem(
                "物数起卦",
                R.drawable.item_number_icon,
                onClick = { navController.navigate(Screen.ItemNumber.route) })
            TabItem(
                "寻找卦宫",
                R.drawable.icon_find,
                onClick = { navController.navigate(Screen.FindBelongTo.route) })
        }
        Row(
            modifier = Modifier
                .fillMaxWidth(),
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
            TabItem(
                "六爻起卦",
                R.drawable.icon_sixline,
                onClick = { navController.navigate(Screen.SixLines.route) })
        }
    }

}

/**
 * 自定义一个图片+文字的tab,实现自定义点击事件
 * @param title 标题
 * @param image 图片
 * @param onClick 点击事件
 */
@Composable
fun TabItem(title: String, image: Int, onClick: () -> Unit, modifier: Modifier? = Modifier) {
    Column(
        modifier = Modifier
            .padding(14.dp)
            .width(60.dp)
            .clickable(
                onClick = onClick,
                indication = LocalIndication.current,
                interactionSource = remember { MutableInteractionSource() })
    ) {
        Image(
            painter = painterResource(image),
            contentDescription = title,
            modifier = Modifier
                .size(40.dp)
                .align(Alignment.CenterHorizontally)
        )
        Text(
            text = title,
            modifier = Modifier.align(Alignment.CenterHorizontally),
            fontSize = 14.sp
        )
    }
}



